/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.rtgen.entitygen.createdtmassmethodgen;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bef.bizentity.GspBusinessEntity;
import com.inspur.edp.bef.bizentity.operation.BizCommonDetermination;
import com.inspur.edp.bef.bizentity.operation.Determination;
import com.inspur.edp.bef.core.determination.builtinimpls.adaptors.BefB4SaveDtmAdaptor;
import com.inspur.edp.bef.core.determination.builtinimpls.adaptors.BefB4SavePredicateDtmAdaptor;
import com.inspur.edp.bef.core.determination.builtinimpls.predicate.ExecutePredicateFactory;
import com.inspur.edp.bef.dtgenerator.common.Utils;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.cef.designtime.api.IGspCommonField;
import com.inspur.edp.cef.designtime.api.operation.CommonDetermination;
import com.inspur.edp.cef.entity.changeset.ChildElementsTuple;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.EntityImplGenContext;
import com.inspur.edp.cef.generator.coreGenerator.base.entitygen.createdtmassmethod.EntityCreateBeforeSaveDtmAssMethodGenerator;
import com.inspur.edp.cef.generator.overall.GenUtils;
import com.inspur.edp.cef.generator.repository.RepositoryGenUtil;
import com.inspur.edp.common.component.actionextension.AttachmentBeforSaveDtmAdaptor;
import com.inspur.edp.common.component.actionextension.DeleteDtmAdaptor;
import com.inspur.edp.common.component.actionextension.ParentTreeDeleteDeterAdaptor;
import com.inspur.edp.common.component.actionextension.TreeBeforeSaveDtmAdaptor;
import com.inspur.edp.common.component.timestamp.CreatedByBeforeSaveDtmAdaptor;
import com.inspur.edp.common.component.timestamp.LastModifiedByBeforeSaveDtmAdaptor;
import com.inspur.edp.common.component.timestamp.TimeStampDtmAdaptor;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.Statement;

public class BefCreateBeforeSaveDtmAssMethodGenerator extends
    EntityCreateBeforeSaveDtmAssMethodGenerator {

  public BefCreateBeforeSaveDtmAssMethodGenerator(EntityImplGenContext entityImplGenContext,
      String nodeCode) {
    super(entityImplGenContext, nodeCode);
  }

  @Override
  protected TypeInfo getNewAdaptor_TypeParam(CommonDetermination op) {
    return CreateDtmAssMethodUtil.anyChildRequestElement(op)
        ? new TypeInfo(BefB4SaveDtmAdaptor.class)
        : new TypeInfo(BefB4SavePredicateDtmAdaptor.class);
  }

  @Override
  public boolean skip() {
    return super.skip() && buildAutoAddingStatements().isEmpty();
  }

  @Override
  protected void buildExtendNewAdaptorParameter(CommonDetermination item, List args) {
    if(CreateDtmAssMethodUtil.anyChildRequestElement(item)) {
      addImport(ChildElementsTuple.class.getTypeName());
      GspBizEntityObject dataType = (GspBizEntityObject) getEntityImplGenContext().getDataType();
      BizCommonDetermination v = (BizCommonDetermination)item;
      args.add(Utils.buildChgTypes(ast, item.getGetExecutingDataStatus()));
      args.add(ast.newBooleanLiteral(v.getRunOnce()));
      if (v.getRequestElements().isEmpty() && v.getRequestChildElements().isEmpty()) {
        return;
      }
      args.add(GenUtils.buildRequestElementsList(ast, getEntityImplGenContext().getDataType(),
          v.getRequestElements()));
      args.add(Utils.buildRequestChildElements(ast, dataType, v.getRequestChildElements()));
    } else {
      addImport(ExecutePredicateFactory.class.getTypeName());
      args.add(CreateDtmAssMethodUtil.buildPredicate(ast, item, getEntityImplGenContext()));
      args.add(ast.newBooleanLiteral(((BizCommonDetermination) item).getRunOnce()));
    }
  }

  @Override
  protected void addExtendingStatements(Block block) {
    block.statements().addAll(buildAutoAddingStatements());
  }

  private List<Statement> autoAddings;
  private List<Statement> buildAutoAddingStatements() {
    if(autoAddings == null) {
      autoAddings = new ArrayList<>();
      getTimeStampDtmGenerators(autoAddings);
      getTreeBeforeSaveDtmGenerators(autoAddings);
      GetParentTreeBeforeSaveDtmGenerators(autoAddings);
      getCreatedByBeforeSaveDtmGenerator(autoAddings);
      getLastModifiedByBeforeSaveDtmGenerator(autoAddings);
      addNewAttachB4Save(autoAddings);
    }
    return autoAddings;
  }

  private void getTimeStampDtmGenerators(List<Statement> block) {
    IGspCommonObject obj = (IGspCommonObject) getEntityImplGenContext().getDataType();
    for (IGspCommonField field : obj.getContainElements()) {
      if (field.getIsUdt() && field.getUdtID().equals("21a5a440-523f-4558-a432-7d71b5e19dfe")) {
        addNoneParCtorAdaptor(block, TimeStampDtmAdaptor.class);
        break;
      }
    }
  }

  private void getTreeBeforeSaveDtmGenerators(List<Statement> block) {
    IGspCommonObject obj = (IGspCommonObject) getEntityImplGenContext().getDataType();
    if (((GspBusinessEntity) obj.getBelongModel()).getEnableTreeDtm()) {
      for (IGspCommonField field : obj.getContainElements()) {
        if (field.getIsUdt() && field.getUdtID().equals("dbfbe55d-ba65-4a7f-a9d4-4f664ec6ec68")) {
          addNoneParCtorAdaptor(block, DeleteDtmAdaptor.class);
          addNoneParCtorAdaptor(block, TreeBeforeSaveDtmAdaptor.class);
          break;
        }
      }
    }
  }

  private void GetParentTreeBeforeSaveDtmGenerators(List<Statement> block) {
    IGspCommonObject obj = (IGspCommonObject) getEntityImplGenContext().getDataType();
    for (IGspCommonField field : obj.getContainElements()) {
      //#TODO 找到分级信息UDT的ID
      if (field.getIsUdt() && field.getUdtID().equals("12be876c-368c-4262-88ab-4112688540b0")) {
        addNoneParCtorAdaptor(block, ParentTreeDeleteDeterAdaptor.class);
        break;
      }
    }
  }

  private void getCreatedByBeforeSaveDtmGenerator(List<Statement> block ) {
    IGspCommonObject obj = (IGspCommonObject) getEntityImplGenContext().getDataType();
    for (IGspCommonField field : obj.getContainElements()) {
      if (field.getIsUdt() && field.getUdtID().equals("91025792-e305-4662-8e80-47cd4980d06f")) {
        addNoneParCtorAdaptor(block, CreatedByBeforeSaveDtmAdaptor.class);
        break;
      }
    }
  }

  private void getLastModifiedByBeforeSaveDtmGenerator(List<Statement> block) {
    IGspCommonObject obj = (IGspCommonObject) getEntityImplGenContext().getDataType();
    for (IGspCommonField field : obj.getContainElements()) {
      if (field.getIsUdt() && field.getUdtID().equals("f5bf18b7-0a9d-4365-8ed8-655d1e1aa70f")) {
        addNoneParCtorAdaptor(block, LastModifiedByBeforeSaveDtmAdaptor.class);
        break;
      }
    }
  }

  private void addNoneParCtorAdaptor(List<Statement> block, Class t) {
    addImport(t.getTypeName());
    ClassInstanceCreation adaptorInstanceCreation = GenUtils.buildClassInstantiation(ast, t);
    MethodInvocation methodInvocation = RepositoryGenUtil
        .createMethodInvocation(ast, ast.newSimpleName(varNameAssembler), "addDetermination", null);
    methodInvocation.arguments().add(adaptorInstanceCreation);
    block.add(ast.newExpressionStatement(methodInvocation));
  }

  private void addNewAttachB4Save(List<Statement> block) {
    IGspCommonObject obj = (IGspCommonObject) getEntityImplGenContext().getDataType();
    for (IGspCommonField field : obj.getContainElements()) {
      if (field.getIsUdt() && field.getUdtID().equals("ee86e96e-6d51-48c7-9e92-9b7dfe3b43b6")) {
        addNoneParCtorAdaptor(block, AttachmentBeforSaveDtmAdaptor.class);
        break;
      }
    }
  }
}

